package com.android.removeClothForBeautyGirl;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class MainActivity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(new MyView(this));
//		setContentView(R.layout.activity_main);
	}

}

class MyView extends View
{
	private int SCREEN_W;
	private int SCREEN_H;

	private Context mContext;
	private Paint mPaint;
	private Path mPath;
	private Canvas mCanvas;
	private Bitmap mBitmap;
	private float mX;
	private float mY;
	private static final float TOUCH_TOLERANCE = 4;

	public MyView(Context context)
	{
		super(context);
		this.mContext = context;
		setFocusable(true);

		setScreenWH();

		Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pre),
		        SCREEN_W, SCREEN_H, true);
		setBitmapResource(bitmap);
		setBackgroundResource(R.drawable.after);

	}

	private void setBitmapResource(Bitmap bitmap)
	{

		mPaint = new Paint();
		mPaint.setAntiAlias(true);// 抗锯齿

		mPaint.setAlpha(0);// 设置透明度 数字越低，越透明
		mPaint.setDither(true);// 防抖动
		mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));// 设置两张图片相交时的模式
		                                                                   // :取两层绘制交集。显示下层。
		mPaint.setStyle(Paint.Style.STROKE);// 设置画笔类型： 实心还是空心
		mPaint.setStrokeJoin(Paint.Join.ROUND);// 设置结合处的样子：Miter:结合处为锐角，
		                                       // Round:结合处为圆弧：BEVEL：结合处为直线。
		mPaint.setStrokeCap(Paint.Cap.ROUND);// 设置画笔的形状：
		mPaint.setStrokeWidth(50);// 设置画笔的空心线宽

		mPath = new Path();

		mBitmap = Bitmap.createBitmap(SCREEN_W, SCREEN_H, Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);
		mCanvas.drawBitmap(bitmap, 0, 0, null);
	}

	private void setScreenWH()
	{
		DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
		SCREEN_W = dm.widthPixels;
		SCREEN_H = dm.heightPixels;
	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		canvas.drawBitmap(mBitmap, 0, 0, null);
		mCanvas.drawPath(mPath, mPaint);
		super.onDraw(canvas);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		float x = event.getX();
		float y = event.getY();

		switch (event.getAction())
		{
			case MotionEvent.ACTION_DOWN:
				touch_start(x, y);//记录路径的起始位置 mPath.moveTo(x,y)
				invalidate();
				break;
			case MotionEvent.ACTION_MOVE:
				touch_move(x, y);//记录路径的移动路径 mPath.quadTo();
				invalidate();
				break;
			case MotionEvent.ACTION_UP:
				touch_up(x, y);//画路径 mCanvas.drawPath(mPath,mPaint);  并清理路径 mPath.reset();
				invalidate();
				// playMusic();
				break;
		}

		return true;
	}

	private void touch_start(float x, float y)
	{
		mX = x;
		mY = y;
		mPath.reset();
		mPath.moveTo(x, y);

	}

	private void touch_move(float x, float y)
	{
		float dx = Math.abs(x - mX);
		float dy = Math.abs(y - mY);
		if (dx > TOUCH_TOLERANCE || dy > TOUCH_TOLERANCE)
		{
			// 二次贝塞尔，实现平滑曲线；previousX, previousY为操作点，cX, cY为终点
			// 设置贝塞尔曲线的操作点为起点和终点的一半
			mPath.quadTo(mX, mY, (mX + x) / 2, (mY + y) / 2);
			mX = x;
			mY = y;
		}

	}

	private void touch_up(float x, float y)
	{
		mPath.lineTo(x, y);
		mCanvas.drawPath(mPath, mPaint);
		mPath.reset();
	}
}
